class Foo {
    std::condition_variable cv;
    std::mutex mtx;
    int k;
public:
    Foo() {
        k=0;
    }

    void first(function<void()> printFirst) {
        printFirst();
        k=1;
        cv.notify_all();
    }

    void second(function<void()> printSecond) {
        
        unique_lock<std::mutex> lock(mtx);
        cv.wait(lock,[&](){return k==1;});
        printSecond();
        k=2;
        cv.notify_all();
    }

    void third(function<void()> printThird) {
        
        unique_lock<std::mutex> lock(mtx);
        cv.wait(lock,[&](){return k==2;});
        printThird();
    }
};
